package com.markul.android.ui.badge

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatus
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MapView
import com.markul.android.R
import com.markul.android.logic.model.BadgeIconData
import com.markul.android.ui.UiTool
import kotlinx.android.synthetic.main.activity_badge.*
import kotlinx.android.synthetic.main.activity_badge.backBtn
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

class BadgeActivity :AppCompatActivity(){

    private var badgeList = ArrayList<Int>()
    private var badgeAdapter = BadgeAdapter(badgeList)
    var handler=Handler()
    var mMapView: MapView? = null
    var mBaiduMap: BaiduMap? = null
    // 用于设置个性化地图的样式文件
    private val CUSTOM_FILE_NAME_CX = "custom_map_config_CX.sty"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_badge)
        //地图初始化
        mMapView = findViewById<View>(R.id.bmapView) as MapView
        mBaiduMap = mMapView!!.map
        mMapView!!.showZoomControls(false)
        mMapView!!.showScaleControl(false)
        //改变地图样式
        changeMapStyle()

        badgeList.addAll(BadgeIconData.BadgeIconList)
        UiTool.hideStatusNavigationBar(this, true)

        BadgeRecycler.layoutManager =
            StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
        BadgeRecycler.adapter = badgeAdapter

        backBtn.setOnClickListener{
            finish()
        }
    }
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.getAction()) {
            MotionEvent.ACTION_UP ->
                //设置延时让画面先加载好
                handler.postDelayed(
                    Runnable {
                        UiTool.hideStatusNavigationBar(this, true)
                    }, 3000
                )
        }
        return super.dispatchTouchEvent(ev)}

    private fun changeMapStyle() {
        // 构建地图状态
        val builder = MapStatus.Builder()
        // 更新地图
        mMapView!!.map.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
        // 获取.sty文件路径
        val customStyleFilePath: String? =
            getCustomStyleFilePath(this, CUSTOM_FILE_NAME_CX)
        // 设置个性化地图样式文件的路径和加载方式
        mMapView!!.setMapCustomStylePath(customStyleFilePath)
        // 动态设置个性化地图样式是否生效
        mMapView!!.setMapCustomStyleEnable(true)
    }

    //获取个性化地图文件地址
    private fun getCustomStyleFilePath(context: Context, customStyleFileName: String): String? {
        var outputStream: FileOutputStream? = null
        var inputStream: InputStream? = null
        var parentPath: String? = null
        try {
            inputStream = context.assets.open("customConfigdir/$customStyleFileName")
            val buffer = ByteArray(inputStream.available())
            inputStream.read(buffer)
            parentPath = context.filesDir.absolutePath
            val customStyleFile = File("$parentPath/$customStyleFileName")
            if (customStyleFile.exists()) {
                customStyleFile.delete()
            }
            customStyleFile.createNewFile()
            outputStream = FileOutputStream(customStyleFile)
            outputStream.write(buffer)
        } catch (e: IOException) {
            Log.e("CustomMapDemo", "Copy custom style file failed", e)
        } finally {
            try {
                inputStream?.close()
                outputStream?.close()
            } catch (e: IOException) {
                Log.e("CustomMapDemo", "Close stream failed", e)
                return null
            }
        }
        return "$parentPath/$customStyleFileName"
    }
}
